Fracture toughness figures

In [8]:
import pandas as pd
import numpy as np
import re
import plotly.express as px
import pickle
from IPython.core.display import display, HTML
import plotly.io as pio
pio.renderers.default = "notebook"

#load saved plot.ly figures
with open('../plotlyfigs/fracture-toughness.pickle','rb') as f:
    fig=pickle.load(f)

#generate plots and csv files
for f in fig:
    arrays = []
    data = []
    x = f['layout']['xaxis']['title']
    xtitle = x
    y = f['layout']['yaxis']['title']
    if y == 'beta':
        ytitle = r'$\beta$'
    else:
        ytitle = y
    title = f['layout']['title']
    for i in range(len(f['data'])):
        arrays.extend([f['data'][i]['name']]*2)
        data.append(f['data'][i]['x'])
        data.append(f['data'][i]['y'])
    arrays = [arrays,[x,y]*len(f['data'])]
    tuples = list(zip(*arrays))
    data_t = [list(j) for j in zip(*data)]
    index = pd.MultiIndex.from_tuples(tuples, names=['name', 'axis'])
    df = pd.DataFrame(data_t, columns=index)
    #export data to csv
    mytitle = 'p' + re.search('\(.* (\d{3})\)',f['layout']['title'])[1] + '.csv'
    df.to_csv(mytitle)
    #plot data
    myfig = px.line(df.stack(0).reset_index(), x=x, y=y, color="name", title=title, labels={x:xtitle, y:ytitle, 'name':'alloy'})
    myfig.show()
    display(HTML("<a href=\""+mytitle+"\">Download .csv</a>"))
In [ ]: